import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { useState } from "react";
import AccountLayout from "../../compoents/account-layout";
import classes from "./changePassword.module.less";
import { useDispatch, useSelector } from "react-redux";
import { Label } from "../../compoents/styled";
import { useLockFn, useSetState } from "ahooks";
import LinearInput from "../../compoents/linear-input";
import VerificationCode from "../../compoents/verifiaction-code";
import classNames from "classnames";
import toast from "../../compoents/toast";
import userServices from "../../hooks/useRequest";
import { forgetPass } from "../../api/user";
import { KEYCONST } from "../../enums";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
function ChangePassword() {
    var userInfo = useSelector(function(state) {
        return state.app.userInfo;
    });
    var _useState = _sliced_to_array(useState(!!userInfo.email), 1), isEmail = _useState[0];
    var _useSetState = _sliced_to_array(useSetState({
        password: "",
        code: "",
        passwordError: false
    }), 2), state = _useSetState[0], setState = _useSetState[1];
    var services = userServices({
        forgetPass: forgetPass
    });
    var dispatch = useDispatch();
    var canClick = state.password.trim().length > 0 && state.code.trim().length > 0 && !state.passwordError;
    var checkPassword = function() {
        if (!state.password) return;
        if (state.password.length < 8 || state.password.length > 16) {
            toast.createToast({
                type: "errToast",
                message: "The password must be at least 8 characters!"
            });
            setState({
                passwordError: true
            });
            return;
        }
        if (!/^[a-zA-Z0-9]{8,16}$/.test(state.password)) {
            toast.createToast({
                type: "errToast",
                message: "The password must be numbers or letters!"
            });
            setState({
                passwordError: true
            });
            return;
        }
        setState({
            passwordError: false
        });
    };
    var updatePWD = useLockFn(/*#__PURE__*/ _async_to_generator(function() {
        var data, e;
        return _ts_generator(this, function(_state) {
            switch(_state.label){
                case 0:
                    if (!canClick) return [
                        2
                    ];
                    if (state.password.trim().length < 8) {
                        toast.createToast({
                            type: "errToast",
                            message: "The password must be at least 8 characters!"
                        });
                        return [
                            2
                        ];
                    }
                    data = {
                        // email:state.email,
                        verificationCode: state.code,
                        password: state.password,
                        email: undefined,
                        areaCode: undefined,
                        phone: undefined,
                        loginName: undefined
                    };
                    if (isEmail) {
                        data.loginName = userInfo.email;
                    } else {
                        data.loginName = "".concat(userInfo.phone);
                    }
                    _state.label = 1;
                case 1:
                    _state.trys.push([
                        1,
                        3,
                        ,
                        4
                    ]);
                    return [
                        4,
                        services.forgetPass(data, false).then(function(res) {
                            if (res.code === 200) {
                                toast.createToast({
                                    message: "Password Changed!"
                                });
                                localStorage.setItem(KEYCONST.token, JSON.stringify(res.data));
                                dispatch.app.PUSH("/home");
                            } else {
                                toast.createToast({
                                    message: res.msg,
                                    type: "errToast"
                                });
                            }
                        })
                    ];
                case 2:
                    _state.sent();
                    return [
                        3,
                        4
                    ];
                case 3:
                    e = _state.sent();
                    return [
                        3,
                        4
                    ];
                case 4:
                    return [
                        2
                    ];
            }
        });
    }));
    return /*#__PURE__*/ _jsx(AccountLayout, {
        children: /*#__PURE__*/ _jsx("div", {
            className: classes.change_password,
            children: /*#__PURE__*/ _jsxs("div", {
                className: classes.view_port,
                children: [
                    /*#__PURE__*/ _jsx("div", {
                        className: classes.title,
                        children: "Change password"
                    }),
                    /*#__PURE__*/ _jsxs("div", {
                        className: classes.desc,
                        children: [
                            "For the security of your account, please enter the verification code sent to ",
                            userInfo.email ? userInfo.email : userInfo.phone
                        ]
                    }),
                    /*#__PURE__*/ _jsxs("div", {
                        className: classes.form,
                        children: [
                            /*#__PURE__*/ _jsxs("div", {
                                className: classes.field,
                                children: [
                                    /*#__PURE__*/ _jsx(Label, {
                                        children: "New password"
                                    }),
                                    /*#__PURE__*/ _jsx(LinearInput, {
                                        value: state.password,
                                        placeholder: "Enter your password",
                                        onChange: function(value) {
                                            return setState({
                                                password: value
                                            });
                                        },
                                        maxlength: 24,
                                        name: "password",
                                        type: "safe-password",
                                        password: true,
                                        onBlur: checkPassword,
                                        error: state.passwordError
                                    })
                                ]
                            }),
                            /*#__PURE__*/ _jsxs("div", {
                                className: classes.field,
                                children: [
                                    /*#__PURE__*/ _jsx(Label, {
                                        children: "Verification"
                                    }),
                                    /*#__PURE__*/ _jsx(VerificationCode, {
                                        target: isEmail ? userInfo.email : userInfo.phone,
                                        errMessage: isEmail ? "email cannot be empty" : "phoneNumber cannot be empty",
                                        value: state.code,
                                        onChange: function(value) {
                                            return setState({
                                                code: value
                                            });
                                        }
                                    })
                                ]
                            })
                        ]
                    }),
                    /*#__PURE__*/ _jsx("div", {
                        onClick: updatePWD,
                        className: classNames(classes.btn, canClick ? "" : classes["btn_disabled"]),
                        children: /*#__PURE__*/ _jsx("span", {
                            children: "Confirm"
                        })
                    })
                ]
            })
        })
    });
}
export default ChangePassword;

 //# sourceMappingURL=index.js.map